package _01_动态数组;

import java.util.Arrays;

/**
 * https://leetcode-cn.com/problems/sort-colors/
 *
 * @Author: haogege
 * @Date: 2021/8/26
 */
public class _75_颜色分类 {


    public static void main(String[] args) {
        int[] nums = {0};
        _75_颜色分类 v = new _75_颜色分类();
        v.sortColors(nums);
        System.out.println(Arrays.toString(nums));
    }

    public void sortColors(int[] nums) {
        int minP = 0;
        int maxP = nums.length - 1;
        int position = minP;
        while (position <= maxP) {
            if (nums[position] == 2) {
                swap(nums, position, maxP--);
            } else if (nums[position] == 0) {
                swap(nums, position++, minP++);
            } else {
                position++;
            }
        }
    }

    public void swap(int[] nums, int index1, int index2) {
        int temp = nums[index1];
        nums[index1] = nums[index2];
        nums[index2] = temp;
    }
}
